package stewardgoodshandler

import (
	"github.com/gin-gonic/gin"
	"yundian/internel/app/dao"
	"yundian/internel/app/helper"
	"yundian/internel/app/helper/request"
	"yundian/internel/app/logic"
	"yundian/internel/app/models/goods"
	"yundian/internel/app/models/paysetting"
	"yundian/internel/pkg/dingo"
	"yundian/internel/pkg/dingo/global"
	. "yundian/internel/pkg/dingo/result"
)

func HandleSetGoodsOrderStatus(ctx *gin.Context) {
	param := &request.SetGoodsOrderStatusReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	param.IsAdmin, _ = helper.GetIsAdminFromCtx(ctx)
	param.LoginUserId, _ = helper.GetUserIdFromCtx(ctx)
	param.ParentId, _ = helper.GetParentIdFromCtx(ctx)

	data := dao.NewGoodsDao().GetAdminGoodsOrdersInfo(param).Unwrap().(*goods.Orders)
	if data.Id <= 0 {
		Response(ctx)("", CodeInvalidParam, false)(OK)
		return
	}

	// 执行退款
	if data.Status != helper.TypeNumGoodsOrderStatusRepeal && param.Status == helper.TypeNumGoodsOrderStatusRepeal {
		if err := logic.NewOrderLogic().HandleWxStewardCancelOrder(&paysetting.WxStewardCancelOrder{PathKey: "goods",
			TransactionId: data.ThirdTradeNo, TradeNo: data.TradeNo, Amount: int64(data.Amount)}); err != nil {
			Response(ctx)("", CodeInvalidParam, false)(OK)
			return
		}
	}

	if dao.NewGoodsDao().SetAdminGoodsOrdersStatus(data, param).Err != nil {
		Response(ctx)("", CodeInvalidParam, false)(OK)
		return
	}

	Response(ctx)("", CodeSuccess, true)(OK)
}

func HandleGetGoodsOrderDetails(ctx *gin.Context) {
	param := &request.GoodsOrderDetailsReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	param.IsAdmin, _ = helper.GetIsAdminFromCtx(ctx)
	param.LoginUserId, _ = helper.GetUserIdFromCtx(ctx)
	param.ParentId, _ = helper.GetParentIdFromCtx(ctx)

	data := dao.NewGoodsDao().GetAdminGoodsOrdersDetailsReq(param)
	Response(ctx)("", CodeSuccess, map[string]interface{}{
		"host": global.Cfg.Ali.AliossPath,
		"data": data.Unwrap(),
	})(OK)
}

// 获取商品订单
func HandleGetGoodsOrderList(ctx *gin.Context) {
	param := &request.GoodsOrderListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	param.IsAdmin, _ = helper.GetIsAdminFromCtx(ctx)
	param.LoginUserId, _ = helper.GetUserIdFromCtx(ctx)
	param.ParentId, _ = helper.GetParentIdFromCtx(ctx)

	data := dao.NewGoodsDao().GetAdminGoodsOrderList(param)
	Response(ctx)("", CodeSuccess, ResponseConfigList{
		Host:       global.Cfg.Ali.AliossPath,
		PageNo:     param.PageNo,
		PageSize:   param.PageSize,
		TotalCount: data.Val().(int64),
		TotalPage:  (data.Val().(int64) + int64(param.PageSize) - 1) / int64(param.PageSize),
		Data:       data.Unwrap(),
	})(OK)
}

// 获取商品列表
func HandleGetGoodsList(ctx *gin.Context) {
	param := &request.GoodsListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	isAdmin, _ := helper.GetIsAdminFromCtx(ctx)
	param.UserId, _ = helper.GetUserIdFromCtx(ctx)
	param.ParentId, _ = helper.GetParentIdFromCtx(ctx)

	data := dao.NewGoodsDao().GetAdminGoodsList(param, isAdmin)
	Response(ctx)("", CodeSuccess, ResponseConfigList{
		Host:       global.Cfg.Ali.AliossPath,
		PageNo:     param.PageNo,
		PageSize:   param.PageSize,
		TotalCount: data.Val().(int64),
		TotalPage:  (data.Val().(int64) + int64(param.PageSize) - 1) / int64(param.PageSize),
		Data:       data.Unwrap(),
	})(OK)
}

// 获取商品列表
func HandleGetGoodsDetails(ctx *gin.Context) {
	param := &request.GoodsListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	data := dao.NewGoodsDao().GetAdminGoodsDetails(param)
	Response(ctx)("", CodeSuccess, map[string]interface{}{
		"data": data.Unwrap(),
		"host": global.Cfg.Ali.AliossPath,
	})(OK)
}

func HandleInsertGoods(ctx *gin.Context) {
	param := &goods.UpdateDb{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	data := dao.NewGoodsDao().GetAdminInsertGoods(param)
	Response(ctx)("", CodeSuccess, data.Unwrap())(OK)
}

func HandleUpdateGoods(ctx *gin.Context) {
	param := &goods.UpdateDb{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	data := dao.NewGoodsDao().GetAdminUpdateGoods(param)
	Response(ctx)("", CodeSuccess, data.Unwrap())(OK)
}

func HandleGoodsSetStatus(ctx *gin.Context) {
	param := &goods.UpdateDb{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	data := dao.NewGoodsDao().GetAdminGoodsSetStatus(param)
	Response(ctx)("", CodeSuccess, data.Unwrap())(OK)
}
